package benlai_miniprogram.benlaiApi;

import benlai_miniprogram.HttpGlobal;
import benlai_miniprogram.JdbcLink;
import benlai_miniprogram.SqlAssert;
import com.alibaba.fastjson.JSONObject;
import ocg.common.re.Assertion;
import benlai_miniprogram.BaseApi;
import ocg.common.re.feed.Feed;
import ocg.common.re.feed.Feeder;
import ocg.http.httpcore.Basic;
import ocg.http.httpcore.Http;
import ocg.http.httpcore.HttpFactory;
import ocg.http.httpcore.HttpModel;
import ocg.http.httpcore.HttpResponse;
import org.testng.annotations.Test;

/**
 * Created by chenbo on 2018/5/9.
 */

public class Token {

    public static JSONObject data;

    /**
        {
             "code": 0,
             "msg": "success",
             "value": {
                 "access_token": "OLpLmsp5rvwt2al6xS-trkpvXNYZiJAtCNylyNL6mY6T12zyJMKrmZTOPMybbDwxTy7IrJtyyYpl9kRfdV0bYuKb6qrkq__MNVEwwQqJ39mVOuMoeyUKTb_Mj50eJwfFvCAp0_BbwWT43Y6ZSWKeOxUYPwBFaZPr7y0rn5DJ_q59axKCMgN00c2Kwi-GgVs21EYTGJEC1AoVJ8EZP7v3zUiWYeSBQFM_uWqaU1AAIUsRivufP5AMoV2PUDWPoQ48s6EJoSMC2Vb397b53dGpRZXQtH08LBVNnWpHw-UOkcAGAyJZpx9QpirYJFaS7MueCABT7k7NieHCWlR0q5xCdSmblbnfoWaHSl7b8dNybzyuK4Mcqi13bhxmwHATOisgNHg5cAfKOE0aD1iyp0if68sMv0JCpBzLi3l1lziZP-pGy4YIWH2qYnKrgosS-kATrnGf8TqkijveHc4-yhHrWBnCrPd_y0zd8tiXBD-SSijXBnKAPphuuLVfnjMA4zFDfFEsHSxjmY604JXBkscbBlrUImTCLWtjcsBPBMY272yUeAwwTfU5i2Nvlwfi75KkXaxRmBtqFBbAOqoggfC6q6B-IniVkHt4Nx0N1tAjy-NCz-r-6zoIOeyyD1rHZTuwdekfQUuYaQuHcSbSajGiAZi5Bh0_lHnznQ0DDT7LnwvPpJugOdGZNsllw6URxLIHWP8hYe_xtFfIetdTX_BWevkUjamrRFefm_X0nIgD47hxAvRn6lMQ8nyPV_l247SSjPNsyaJybhcYXemjqfs6n9wIndo",
                 "token_type": "bearer",
                 "expires_in": "7199",
                 "refresh_token": "fa22b5037c3748b49c39eccd06020f83",
                 "as:client_id": "benlai_miniprogram",
                 "customerId": "15858585858",
                 "nickName": "本来测试",
                 "phone": "158****5858",
                 "gender": "1",
                 "city": "Hongkou",
                 "province": "Shanghai",
                 "country": "China",
                 "avatarUrl": "https://wx.qlogo.cn/mmopen/vi_32/gRtEud3Tl0lm3LIhOuJ0zvmdLz0wW7GeWwKgG85kvFiaBGVRn8MWajXbFxYM302oh0vhhRl1vibu3uML8srpgGcA/0",
                 ".issued": "Thu, 10 May 2018 06:52:37 GMT",
                 ".expires": "Thu, 10 May 2018 08:52:37 GMT"
                 }
        }
     */
    public static void token( String unionId ){

        if (unionId==null || unionId.equals ( "" )){
            unionId = "oSPcEj-RsDFeexVKEO4Ei-EraQFQ";
        }

        HttpModel model = Http.post ( HttpGlobal.Path.Token )
                .header ( "Authorization","Basic YmVubGFpX21pbmlwcm9ncmFtOjdhOTRmMTkwNjA5MzExZTdhNmYxNWZiMDlmNWY4Y2Yw" )
                .parameter ( "grant_type" ,"wx_miniprogram")
                .parameter ( "code","benlaiTest|"+unionId )
                .parameter ( "encryptedData","1111" )
                .parameter ( "iv","111" )
                .parameter ( "systemInfo","{\"model\":\"iPhone 6\",\"pixelRatio\":2,\"windowWidth\":375,\"windowHeight\":555,\"system\":\"iOS 10.0.1\",\"language\":\"zh_CN\",\"version\":\"6.6.3\",\"batteryLevel\":97,\"screenWidth\":375,\"screenHeight\":667,\"SDKVersion\":\"1.9.91\",\"brand\":\"devtools\",\"fontSizeSetting\":16,\"statusBarHeight\":20,\"platform\":\"devtools\"}" )
                .parameter ( "version", HttpGlobal.getVersion () )
                .parameter ( "citySysNo", HttpGlobal.getCitySysNo () )
                .parameter ( "webSiteSysNo", HttpGlobal.getWebSiteSysNo () );

        HttpResponse response = HttpFactory.factory ( model );

        JSONObject res = response.jsonBody ();

        int code = res.getIntValue ( "code" );

        String msg = res.getString ( "msg" );

        data = res.getJSONObject ( "value" );

        if ( data != null) {
            String access_token = data.getString ( "access_token" );
            String token_type = data.getString ( "token_type" );

            Basic.setHeader ( "Authorization" , token_type + " " + access_token );

            if (JdbcLink.isDB_Check ()) {

                String customerId = data.getString ( "customerId" );

                String sql = "select customerId from benlaisales..customer \n" +
                        "where SYSNO = (\n" +
                        "select top 1 customerSYSNO from BenlaiSales..Customer_thirdLogin where loginproviderId='"+unionId+"'\n" +
                        ")";

                Feed feed = SqlAssert
                        .feed ( sql );

                String customerId_s = feed
                        .feeder ( 0 )
                        .get ( "customerId" )
                        .toString ();

                Assertion.isEquals ( customerId_s ,customerId, "微信小程序登录成功" );

            }

        } else {
            Assertion.isEquals ( msg , "success" , "微信小程序登录失败" );
        }
    }

}
